package com.jcfk.eam.controller.mould;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jcfk.common.core.exception.CustomException;
import com.jcfk.common.web.domain.RetResult;
import com.jcfk.eam.api.MdmServiceFegin;
import com.jcfk.eam.domain.dto.main.EamPointCheckResultDTO;
import com.jcfk.eam.domain.dto.mould.MdmMaterialDTO;
import com.jcfk.eam.domain.dto.mould.MouldLedgerOperateDTO;
import com.jcfk.eam.domain.po.main.EamAssetInfo;
import com.jcfk.eam.service.base.EamSopStandardService;
import com.jcfk.eam.service.main.EamAssetInfoService;
import com.jcfk.eam.service.main.EamPointCheckOrderService;
import com.jcfk.eam.service.mould.MouldAssetInfoService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 模具安装管理
 */
@RestController
@RequestMapping("modInstallCheck")
public class MouldnstallController {

    @Autowired
    private EamAssetInfoService eamAssetInfoService;

    @Autowired
    private EamSopStandardService eamSopStandardService;


    @Autowired
    private EamPointCheckOrderService eamPointCheckOrderService;


    @Autowired
    private MdmServiceFegin mdmServiceFegin;


    @Autowired
    private MouldAssetInfoService mouldAssetInfoService;

    /**
     * 根据code模具安装列表
     */
    @ApiOperation("根据code模具安装列表")
    @GetMapping("/getProjectMouldlist/{code}")
    public RetResult<?> getProjectMouldlist(@PathVariable("code") String code) {
        MouldLedgerOperateDTO dto = mouldAssetInfoService.getProjectMouldlist(code);
        QueryWrapper<EamAssetInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(EamAssetInfo::getAssetCode, code);
        EamAssetInfo eamAssetInfo = eamAssetInfoService.getOne(queryWrapper);
        dto.setEamAssetInfo(eamAssetInfo);

        if (eamAssetInfo == null) {
            throw new CustomException("根据该模具编码未检索到模具记录!");
        }


        return RetResult.ok(dto);
    }

    /**
     * 模具安装
     */
    @ApiOperation("模具安装")
    @PostMapping("/install")
    public RetResult<?> install(@RequestBody MouldLedgerOperateDTO dto) {
        EamAssetInfo eamAssetInfo = eamAssetInfoService.getById(dto.getEamAssetInfo().getId());

        if (eamAssetInfo == null) {
            throw new CustomException("模具记录不能为空!");
        }

//        if (eamAssetInfo.getStorageStatus() != 2) {
//            throw new CustomException("该工装不在线边库，无法操作!");
//        }

        // if (dto.getProjectMouldDTO() == null) {
        //     throw new CustomException("请选择模具产品!");
        // }

//        if (eamAssetInfo.getStorageStatus() == 3) {
//            throw new CustomException("该工装库存状态已经到设备，无法操作!");
//        }

        mouldAssetInfoService.installMod(dto);
        return RetResult.ok();
    }

    /**
     * 根据code模具拆卸相关信息
     */
    @ApiOperation("根据code模具拆卸相关信息")
    @GetMapping("/getstriplist/{code}")
    public RetResult<?> getStripList(@PathVariable("code") String code) {

        return RetResult.ok(mouldAssetInfoService.getStripList(code));
    }

    /**
     * 拆卸
     */
    @ApiOperation("拆卸")
    @PostMapping("/strip")
    public RetResult<?> strip(@RequestBody MouldLedgerOperateDTO dto) {
        mouldAssetInfoService.strip(dto);
        return RetResult.ok();
    }

    /**
     * 获取模具信息及安装点检标准
     */
    @ApiOperation("获取模具信息及安装点检标准")
    @GetMapping("/getSopStandard/{code}")
    public RetResult<MouldLedgerOperateDTO> getSopStandard(@PathVariable("code") String code) {
        //获取模具物料信息
        MouldLedgerOperateDTO dto = new MouldLedgerOperateDTO();
        String sapCode = code.substring(0, 7);
        dto.setSapCode(sapCode);
        String projectCode = code.substring(7, 11);
        dto.setProjectCode(projectCode);
        String dataString = code.substring(11, 19);
        dto.setDataString(dataString);
        String processCode = code.substring(code.length() - 3);
        dto.setProcessCode(processCode);
        dto.setCode(code);

        //尺寸点检标准
        EamPointCheckResultDTO eamSopStandardDTO = eamSopStandardService.getSopStandard("TeInstallInspect",sapCode);
        dto.setEamSopStandardDTO(eamSopStandardDTO);

        RetResult<MdmMaterialDTO> mdmMaterialDTORetResult = mdmServiceFegin.getMdmmaterial(sapCode);
        dto.setMdmMaterialDTO(mdmMaterialDTORetResult.getData());

        //获取模具台账信息
        QueryWrapper<EamAssetInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(EamAssetInfo::getAssetCode, code);
        EamAssetInfo eamAssetInfo = eamAssetInfoService.getOne(queryWrapper);
        dto.setEamAssetInfo(eamAssetInfo);

        return RetResult.ok(dto);
    }

    /**
     * 保存安装点检
     */
    @ApiOperation("保存安装点检")
    @PostMapping("/saveCheck")
    public RetResult<?> saveCheck(@RequestBody MouldLedgerOperateDTO dto) {
        // 台账入库/操作记录
        if (dto.getEamAssetInfo() == null) {
            throw new CustomException("模具数据不能为空!");
        }
        // 保存安装点检
        mouldAssetInfoService.saveCheckOrder(dto.getEamAssetInfo(), dto.getEamSopStandardDTO(), 2);
        return RetResult.ok();
    }
}
